package clientController;

import implementations.PointImpl;
import interfaces.IGamePanel;
import interfaces.IPlayer;
import interfaces.IPoint;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;

import javax.swing.JFrame;

public class Client
{
	private static int id = 0;
	private static String ip = null;
	private static int port = 0;
	//private static String playerName;
	private static IPlayer player;

	public static void createClient(int id, String ip, int port,
			IPlayer player)
	{
		Client.player=player;
		//Client.playerName = player.getName();
		Client.port = port;
		Client.ip = ip;
		Client.id = id;
		String hostName;
		int portNumber = port;
		// if (ip.compareTo("-1") == 0)
		// {
		// hostName = "127.0.0.1";
		// portNumber=82;
		// }
		// else if (ip.compareTo("0") == 0)
		// System.out.println("Don't conn");
		// else
		// {
		hostName = ip;

		// portNumber = 81;
		System.out.println("Client:ip&port:" + hostName + " " + portNumber);
		try
		{
			Socket echoSocket = new Socket(hostName, portNumber);
			System.out.println("Client:connected");
			PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
					true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					echoSocket.getInputStream()));
			// BufferedReader stdIn = new BufferedReader(new
			// InputStreamReader(
			// System.in));
			String myIp = InetAddress.getLocalHost().getHostAddress();
			out.println(id);
			out.println(port);
			out.println(myIp);
			String str = in.readLine();
			System.out.println("Client:Response from server:" + str);
			out.close();
			in.close();
			echoSocket.close();
			System.out.println("Client: out!");

		} catch (Exception e)
		{

		}
		// }
	}

	public static void clientAttack(int id, IPoint point)
	{
		// String hostName = "127.0.0.1";
		// int portNumber = 81;

		try
		{
			System.out.println("Client:Attacking!");
			Socket echoSocket = new Socket(ip, port);
			PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
					true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					echoSocket.getInputStream()));
			// BufferedReader stdIn = new BufferedReader(new InputStreamReader(
			// System.in));
			out.println(id);
			out.println(point.getX() + ";" + point.getY());
			String resp = in.readLine();
			boolean resp1=false;
			if(resp.compareTo("true")==0)
				resp1=true;
			System.out.println("RESP CEVA:"+resp1);
			player.getAttackResponse(point,resp1);
			//player.getAttackResponse(point, resp1);
			// if (alala)
			System.out.println("Client:Response for attacking:" + resp1);

			echoSocket.close();
		} catch (Exception e)
		{

		}
	}

	public static void playerRdy()
	{
		try
		{
			System.out.println("Client:player RDY");
			Socket echoSocket = new Socket(ip, port);
			PrintWriter out = new PrintWriter(echoSocket.getOutputStream(),
					true);
			BufferedReader in = new BufferedReader(new InputStreamReader(
					echoSocket.getInputStream()));
			// BufferedReader stdIn = new BufferedReader(new InputStreamReader(
			// System.in));
			out.println(id);
			out.println("-2");
			echoSocket.close();
			System.out.println("Client:out!");
		} catch (Exception e)
		{

		}
	}
//	public static void repaint(IGamePanel panel)
//	{
//		panel.getThisFrame().revalidate();
//		panel.getThisFrame().repaint();
//	}
}
